﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.IO;


/// <summary>
/// Summary description for CenterService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
    
public class CenterService : System.Web.Services.WebService {

    public CenterService () {
 
    }
    [WebMethod]
    public bool category_updateSort(string lstKey)
    {
        try
        {
            string[] arrKey = lstKey.Split('|');
            using (new SessionScope())
            {
                for (int i = 0; i < arrKey.Length - 1; i++)
                {
                    int order = ConvertUtility.ToInt32(arrKey[i].Split(':')[0]);
                    int id = ConvertUtility.ToInt32(arrKey[i].Split(':')[1]);
                    Category_Info catInfo = Category_Info.Find(id);
                    catInfo.catOrder = order;
                    catInfo.Save();
                }
            }
            return true;
        }
        catch (Exception ex) { return false; }

    }
    [WebMethod]
    public bool contact_updateSort(string lstKey)
    {
        try
        {
            string[] arrKey = lstKey.Split('|');
            using (new SessionScope())
            {
                for (int i = 0; i < arrKey.Length - 1; i++)
                {
                    int order = ConvertUtility.ToInt32(arrKey[i].Split(':')[0]);
                    int id = ConvertUtility.ToInt32(arrKey[i].Split(':')[1]);
                    Content_Info contentInfo = Content_Info.Find(id);
                    contentInfo.contentOrder = order;
                    contentInfo.SaveAndFlush();
                }
            }
            return true;
        }
        catch (Exception ex) { return false; }

    }
    [WebMethod]
    public bool sendNewsLeter(int  id)
    {
        try
        {
            Newsletter_Info newsLeterInfo = Newsletter_Info.Find(id);
            string subject = newsLeterInfo.subject;
            string body = newsLeterInfo.noidung;
            string sendFrom = "unreply.notification@gmail.com";
            string sendTo = "";
            string sendCC = "";

            memberLet_Info[] lstMem = memberLet_Info.FindAll(Expression.Eq("isActive", true));
            if (lstMem.Length > 0)
            {
                sendTo = lstMem[0].email;
                for (int i = 1; i < lstMem.Length; i++)
                {
                    sendCC += lstMem[i].email + "-";
                    App_Mail.Send_Email(sendFrom, sendTo, sendCC, subject, body, "unreply.notification@gmail.com", "d0983034303");
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    [WebMethod(CacheDuration = 30)]
    public string getPhuong(int quanID)
    {
        try
        {
            using (new SessionScope())
            {
                string result = "";
                Category_Info[] lstCat = Utils.getPhuong(quanID);
                foreach (Category_Info cat in lstCat)
                {
                    if (cat != null)
                        result += cat.catName + ", ";
                }
                return result;
            }
        }
        catch (Exception ex) { return ""; }
    }
    [WebMethod(CacheDuration = 30)]
    public string getTenDuong(int phuongID)
    {
        try
        {
            using (new SessionScope())
            {
                //string result = "";
                //Category_Info[] lstCat = Utils.getTenDuong(phuongID);
                //foreach (Category_Info cat in lstCat)
                //{
                //    if (cat!=null)
                //        result += cat.catName + ", ";
                //}
                //return result;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                Category_Info[] lstTenDuong = Utils.getTenDuong(phuongID);
                return jss.Serialize(lstTenDuong);
            }
        }
        catch (Exception ex) { return ""; }
    }
}
   

